using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
using DIS_DAL_WS.Models;

namespace DIS_DAL_WS.DTO
{
    [DataContract]
	public class OrdersDTO
	{
        [DataMember]
		public int ID { get; set; }
        [DataMember]
		public int IDRestaurant { get; set; }
        [DataMember]
		public DateTime OrderDate { get; set; }
        [DataMember]
		public bool IsUsed { get; set; }
        [DataMember]
		public bool IsPaid { get; set; }
        [DataMember]
        public RestaurantDTO Restaurant { get; set; }
        [DataMember]
        List<OrderDetailDTO> OrderDetails { get; set; }

        public static OrdersDTO Translate(Orders Entity)
        {
            if (Entity != null)
            {
                var dto = new OrdersDTO
                {
                    ID = Entity.ID,
                    OrderDate = Entity.OrderDate == null ? DateTime.MinValue : Entity.OrderDate.Value,
                    IsUsed = Entity.IsUsed == null ? false : Entity.IsUsed.Value,
                    IsPaid = Entity.IsPaid == null ? false : Entity.IsPaid.Value,
                    Restaurant = RestaurantDTO.TranslateBase(Entity.Restaurant),
                    OrderDetails = new List<OrderDetailDTO>()
                };
                if (Entity.IDRestaurant != null)
                {
                    dto.IDRestaurant = Entity.IDRestaurant.Value;
                }
                if (Entity.OrderDetail != null)
                {
                    foreach (var item in Entity.OrderDetail)
                    {
                        dto.OrderDetails.Add(OrderDetailDTO.Translate(item));
                    }
                }
                return dto;
            }
            return null;
        }

        public static OrdersDTO TranslateBase(Orders Entity)
        {
            if (Entity != null)
            {
                var dto = new OrdersDTO
                {
                    ID = Entity.ID,
                    OrderDate = Entity.OrderDate == null ? DateTime.MinValue : Entity.OrderDate.Value,
                    IsUsed = Entity.IsUsed == null ? false : Entity.IsUsed.Value,
                    IsPaid = Entity.IsPaid == null ? false : Entity.IsPaid.Value,
                    Restaurant = RestaurantDTO.TranslateBase(Entity.Restaurant)
                };
                if (Entity.IDRestaurant != null)
                {
                    dto.IDRestaurant = Entity.IDRestaurant.Value;
                }
                else
                {
                    dto.IDRestaurant = -1;
                }
                return dto;
            }
            return null;
        }

        public static Orders Reverse(OrdersDTO obj)
        {
            var entity = new Orders
            {
                ID = obj.ID,
                OrderDate = obj.OrderDate,
                IsUsed = obj.IsUsed,
                IsPaid = obj.IsPaid
            };
            if (obj.IDRestaurant != -1)
            {
                entity.IDRestaurant = obj.IDRestaurant;
            }
            return entity;
        }

        public void UpdateEntity(ref Orders entity)
        {
            entity.OrderDate = OrderDate;
            entity.IsUsed = IsUsed;
            entity.IsPaid = IsPaid;
            if (IDRestaurant != -1)
            {
                entity.IDRestaurant = IDRestaurant;
            }
        }
	}
}